package com.ymatou.iapi.idcardquery.resp;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.idcardmng.parameter.DeleteIdCardInfoBean;
import com.ymatou.iapi.idcardmng.service.DeleteIdCardInfoCall;

public class IdCardInfoVerify {

	public static String convertPicUrl(String picUrl) {
		String[] picUrls = picUrl.split(";");
		return picUrls[0].replace("_o", "_s");
	}

	/** 下载图片,转换成base64格式
	 * 
	 * @param picUrl
	 * @return
	 */
	public static String downloadPic(String picUrl) {

		URL url = null;
		try {
			url = new URL(picUrl);
			DataInputStream dataInputStream = new DataInputStream(
					url.openStream());
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = dataInputStream.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
			byte[] context = output.toByteArray();

			Class<?> clazz = Class
					.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
			Method mainMethod = clazz.getMethod("encode", byte[].class);
			mainMethod.setAccessible(true);
			Object retObj = mainMethod.invoke(null, new Object[]{context});
			return (String) retObj;

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 从本地库删除身份证信息
	 * @param buyerId
	 * @param name
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void callDeleteIdCardInfo(long buyerId, String name)
			throws ClientProtocolException, IOException {

		DeleteIdCardInfoBean deleteidcardinfoBean = new DeleteIdCardInfoBean();
		DeleteIdCardInfoCall deleteidcardinfoCall = new DeleteIdCardInfoCall();

		deleteidcardinfoBean.setBuyerId(buyerId);
		deleteidcardinfoBean.setName(name);

		deleteidcardinfoCall.setData(deleteidcardinfoBean);
		deleteidcardinfoCall.callService();

	}
}
